package com.zsc.zojbackendjudgeservice.judge.codesandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import com.zsc.zojbackendcommon.common.ErrorCode;
import com.zsc.zojbackendcommon.exception.BusinessException;
import com.zsc.zojbackendjudgeservice.judge.codesandbox.CodeSendBox;
import com.zsc.zojbackendmodel.codesendbox.ExecuteCodeRequest;
import com.zsc.zojbackendmodel.codesendbox.ExecuteCodeResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * 远程调用代码沙箱
 */
public class RemoteCodeSendBox implements CodeSendBox {
    @Override
    public ExecuteCodeResponse ExecuteCode(ExecuteCodeRequest executeCodeRequest) {
        System.out.println("远程调用代码沙箱");
        String jsonStr = JSONUtil.toJsonStr(executeCodeRequest);
        String responseStr = HttpUtil.createPost("http://localhost:8090/executeCode")
                .body(jsonStr)
                .execute()
                .body();
        if(StringUtils.isBlank(responseStr)){
            throw new BusinessException(ErrorCode.API_REMOTE_ERROR,"调用代码沙箱失败");
        }
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
